﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using WeBlog.Interfaces;
using WeBlog.Repositories;
using WeBlog.Services;

namespace WeBlog.Models {
    public class PostFormViewModel {       
        public IEnumerable<SelectListItem> Authors { get; set; }
        public IEnumerable<SelectListItem> Categories { get; set; }
        public IEnumerable<Tag> Tags { get; set; }
        public string TagString { get; set; }
        
        private readonly PostRepository _postRepository;
        private readonly UserRepository _userRepository;
        private readonly CategoryRepository _categoryRepository;
        private readonly TagRepository _tagRepository;

        private Post _post;
        public Post Post {
            get { return _post; }
            set{
                _post = value;
                if( value != null && _post.Tags != null  )
                    TagString = String.Join(" ", _post.Tags.Select(x => x.Name));
            }
        }

        public PostFormViewModel( IUnitOfWork unitOfWork, User user  )
        {
            
            _postRepository = new PostRepository(unitOfWork);
            _userRepository = new UserRepository(unitOfWork);
            _categoryRepository = new CategoryRepository(unitOfWork);
            _tagRepository = new TagRepository(unitOfWork);

            var configuration = SiteManager.CurrentConfiguration;

            Post = new Post { 
                PublishDate = DateTime.Now, 
                Author = user,
                AreCommentsEnabled = configuration.AreCommentsEnabled
            };

            if (user.IsInRole(Role.Editor) || user.IsInRole(Role.Admin))
            {
                Authors = _userRepository.FindAuthors()
                    .Select(x => new SelectListItem { Text = x.FullName, Value = x.UserId.ToString(), Selected = (x.UserId == (Post == null ? -1 : Post.AuthorId)) });
            }
            else
            {
                Authors = new List<SelectListItem>() { new SelectListItem { Text = user.FullName, Value = user.UserId.ToString(), Selected = true } };
            }

            Categories = _categoryRepository.FindAll().ToList()
                .Select(x => new SelectListItem { Text = x.Name, Value = x.CategoryId.ToString() });

            Tags = _tagRepository.FindAll();
        }

        public PostFormViewModel(IUnitOfWork unitOfWork, User user, int id)
            : this(unitOfWork, user )
        {
            Post = _postRepository.Find(id);
            if (Post == null) throw new NotFoundException();

            Categories = Categories
                .Select(x => new SelectListItem {
                                     Text = x.Text,
                                     Value = x.Value,
                                     Selected = Post.Categories.Any(c => c.CategoryId.ToString() == x.Value)
                                 });
        }
    }
}